/**
 * Copyright (c) 2009 Netflix-Flex_API Contributors.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of 
 * this software and associated documentation files (the "Software"), to deal in 
 * the Software without restriction, including without limitation the rights to 
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 
 * of the Software, and to permit persons to whom the Software is furnished to do 
 * so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all 
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE. 
 * */
package com.netflix.webapis.authenticate
{
	import com.netflix.webapis.ServiceBase;
	import com.netflix.webapis.ServiceFault;
	import com.netflix.webapis.authenticate.events.AccessTokenFaultEvent;
	import com.netflix.webapis.authenticate.events.AccessTokenResultEvent;
	
	import mx.rpc.AsyncToken;
	import mx.rpc.Responder;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;
	
	import org.iotashan.oauth.OAuthRequest;
	import org.iotashan.oauth.OAuthToken;

	/**
	* Result Event.
	*/	
	[Event(name="result",type="com.netflix.webapis.authenticate.events.AccessTokenResultEvent")]
	/**
	* Fault Event.
	*/	
	[Event(name="fault",type="com.netflix.webapis.authenticate.events.AccessTokenFaultEvent")]
	
	/**
	 * Service to requests an Access Token with Netflix to connect to a 
	 * specific user's account.
	 * @author jonbcampos
	 * 
	 */	
	public class AccessTokenService extends ServiceBase
	{
		public function AccessTokenService()
		{
			super();
		}
		
		/**
		 * Requests an Access Token with Netflix to connect to a 
		 * specific user's account. Returns the OAuth Token and 
		 * the user's id that this application is authenticated for. 
		 * 
		 */		
		public function getAccessToken():void
		{
			var service:HTTPService = new HTTPService();
			service.addEventListener(ResultEvent.RESULT,this.onResult);
			service.addEventListener(FaultEvent.FAULT,this.onFault);
			var requestToken:OAuthToken = new OAuthToken(token,tokenSecret);
			var tokenRequest:OAuthRequest = new OAuthRequest("GET",NETFLIX_BASE_URL+"oauth/access_token",null,consumer,requestToken);
			service.url = tokenRequest.buildRequest(SIG_METHOD);
			service.send();
		}
		
		/**
		 * @private
		 * Receives service result and dispatches result event. 
		 * @param event
		 * 
		 */		
		private function onResult(event:ResultEvent):void
		{
			var a:Array = String(event.result).split("&");
			var s:Array;
			var o:Object = new Object();
			for each(var item:String in a)
			{
				s = item.split("=");
				o[s[0]] = s[1];
			}
			
			token = o.oauth_token as String;
			tokenSecret = o.oauth_token_secret as String;
			userId = o.user_id;
			
			dispatchEvent(new AccessTokenResultEvent(AccessTokenResultEvent.RESULT,accessToken,userId));
		}
		
		/**
		 * @private
		 * Dispatches fault event.  
		 * @param event
		 * 
		 */		
		private function onFault(event:FaultEvent):void
		{
			dispatchEvent(new AccessTokenFaultEvent(AccessTokenFaultEvent.FAULT,new ServiceFault(event.fault.faultCode,event.fault.faultString,event.fault.faultDetail)));
		}

	}
}